/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.List;

import org.hibernate.Query;

import com.shine.icis.dao.ISubmitConfDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.SubmitCalendar;
import com.shine.icis.hbean.SubmitConf;

/**
 * @author zsy
 *
 */
public class SubmitConfDAO extends BaseHibernateDAO implements ISubmitConfDAO {
	
	@Override
	public SubmitConf findById(String id) {
		SubmitConf cfg = (SubmitConf)getSession().get(SubmitConf.class, id);
		return cfg;
	}
	
	@Override
	public SubmitConf findByCode(String code, String type) {
		Query query = getSession().createQuery("from SubmitConf as cfg where cfg.type=:type and cfg.code=:code");
		query.setString("type", type);
    	query.setString("code", code);
    	query.setMaxResults(1);
    	return (SubmitConf)query.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SubmitConf> findByType(String type, String status) {
		String hql = "from SubmitConf as cfg where cfg.type=:type";
		if (status != null) {
			hql += " and cfg.status=:status";
		}
		hql += " order by cfg.displayOrder";
    	Query query = getSession().createQuery(hql);
    	query.setString("type", type);
    	if (status != null) {
    		query.setString("status", status);
    	}
    	return query.list();
    }

//	@SuppressWarnings("unchecked")
//	@Override
//	public List<DictOption> findDictOptions(String groupCode) {
//		Query query = getSession().getNamedQuery("SubmitConfDAO.findDictOptions");
//		query.setString(0, groupCode);
//		List<Object[]> val = query.list();
//		List<DictOption> ops = new ArrayList<DictOption>(val.size());
//		for (Object[] oArr : val) {
//			DictOption op = new DictOption();
//			op.setKey(String.valueOf(oArr[0]));
//			op.setText(String.valueOf(oArr[1]));
//			ops.add(op);
//		}
//		return ops;
//	}
	
	@Override
	public String findCompanyCode(String busiDate) {
		Query query = getSession().getNamedQuery("SubmitConfDAO.findCompanyCode");
		query.setString("busiDate", busiDate);
		query.setMaxResults(1);
		Object o = query.uniqueResult();
		return o == null ? null : o.toString();
	}
	
	@Override
	public int findPreviousTradeDate(int date) {
		Query query = getSession().getNamedQuery("SubmitConfDAO.findPreviousTradeDate");
		query.setInteger(0, date);
		query.setMaxResults(1);
		Integer d = (Integer)query.uniqueResult();
		return d == null ? 0 : d;
	}
	
	@Override
	public int findNextTradeDate(int date) {
		Query query = getSession().getNamedQuery("SubmitConfDAO.findNextTradeDate");
		query.setInteger(0, date);
		query.setMaxResults(1);
		Integer d = (Integer)query.uniqueResult();
		return d == null ? 0 : d;
	}
	
	@Override
	public SubmitCalendar findSubmitCalendar(int date) {
		Query query = getSession().getNamedQuery("SubmitConfDAO.findSubmitCalendar");
		query.setInteger(0, date);
		query.setMaxResults(1);
		Object[] or = (Object[])query.uniqueResult();
		SubmitCalendar sc = new SubmitCalendar();
		sc.setDateId((Integer)or[0]);
		sc.setIncrementFlag((Integer)or[1]);
		sc.setTradeFlag((Integer)or[2]);
		return sc;
	}
}
